package game.web.base;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import projectx.core.base.ProjectXSystemException;
import projectx.core.base.ProjectXUserException;
import projectx.db.ProjectXSQLException;
import projectx.transaction.DatabaseTransaction;
import projectx.transaction.TransactionHandler;
import projectx.util.StringUtil;

public final class LoginUserTrx extends DatabaseTransaction {

	// --------------------------------------------------------------------------
	// S T A T I C M E T H O D S
	//
	// --------------------------------------------------------------------------

	public static User execute(String userId) throws ProjectXUserException,
			ProjectXSystemException {

		return (User) TransactionHandler.call(new LoginUserTrx(userId));
	}

	// --------------------------------------------------------------------------
	// M E M B E R S
	//
	// --------------------------------------------------------------------------
	private final String userid;

	// --------------------------------------------------------------------------
	// C O N S T R U C T O R S
	//
	// --------------------------------------------------------------------------
	private LoginUserTrx(String userid) {

		super(userid);
		this.userid = userid;
	}

	public Object execute() throws ProjectXUserException,
			ProjectXSystemException {
		User user = select();
		return user;
	}

	public enum field {
		userid, username, firstpass, secpass,truename,contactno, level, charge, withdraw, bonus, returnmoney, winmoney, money, agentmoney, returnpos, profit, loginflag, logintimes, createtime, updatetime, createip, updateip;
	}

	private User select() throws ProjectXUserException, ProjectXSQLException {

		String sql = "select userid,username,firstpass,secpass,truename,contactno,level,charge,"
				+ "withdraw,bonus,returnmoney,winmoney,money,agentmoney,"
				+ "returnpos,profit,loginflag,logintimes,createtime,"
				+ "updatetime,createip,updateip from hk_user WHERE userid = ?"; 

		PreparedStatement ps = null;
		ResultSet rs = null;

		System.err.println("LoginUser--->SQL="+sql);
		
		try { 

			if (_log.isDebugEnabled()) {
				_log.debug(sql);
			}

			ps = _conn.prepareStatement(sql);
			ps.setString(1, userid);

			rs = ps.executeQuery();

			if (rs.next()) {

				User user = new User();
				user.setUserid(userid);

				String id = getString(rs.getString(field.userid.ordinal() + 1));
				String username = getString(rs.getString(field.username.ordinal() + 1));
				String firstpass = getString(rs.getString(field.firstpass.ordinal() + 1));
				String secpass = getString(rs.getString(field.secpass.ordinal() + 1));
				String truename = getString(rs.getString(field.truename.ordinal() + 1));
				String contactno = getString(rs.getString(field.contactno.ordinal() + 1));
				double charge = rs.getDouble(field.charge.ordinal() + 1);
				double withdraw = rs.getDouble(field.withdraw.ordinal() + 1);
				double bonus = (rs.getDouble(field.bonus.ordinal() + 1));
				double returnmoney = (rs.getDouble(field.returnmoney.ordinal() + 1));
				double winmoney = (rs.getDouble(field.winmoney.ordinal() + 1));
				double money = (rs.getDouble(field.money.ordinal() + 1));
				int loginflag = (rs.getInt(field.loginflag.ordinal() + 1));
				int logintimes = (rs.getInt(field.logintimes.ordinal() + 1));
				int level = (rs.getInt(field.level.ordinal() + 1));
				double agentmoney = (rs.getDouble(field.agentmoney.ordinal() + 1));
				double returnpos = (rs.getDouble(field.returnpos.ordinal() + 1));
				double profit = (rs.getDouble(field.profit.ordinal() + 1));
				Date createtime = (rs.getDate(field.createtime.ordinal() + 1));
				Date updatetime = (rs.getDate(field.updatetime.ordinal() + 1));
				String createip = getString(rs.getString(field.createip.ordinal() + 1));
				String updateip = getString(rs.getString(field.updateip.ordinal() + 1));
				user.setUserid(userid);
				user.setUsername(username);
				user.setFirstpass(firstpass);
				user.setTruename(truename);
				user.setContactno(contactno);
				user.setLevel(level);
				user.setSecpass(secpass);
				user.setCharge(charge);
				user.setWithdraw(withdraw);
				user.setBonus(bonus);
				user.setReturnmoney(returnmoney);
				user.setWinmoney(winmoney);
				user.setMoney(money);
				user.setAgentmoney(agentmoney);
				user.setReturnpos(returnpos);
				user.setProfit(profit);
				user.setLoginflag(loginflag);
				user.setLogintimes(logintimes);
				user.setCreatetime(createtime);
				user.setUpdatetime(updatetime);
				user.setCreateip(createip);
				user.setUpdateip(updateip);

				return user;
			} else {
				throw new ProjectXUserException("E8032", userid);
			}
		} catch (SQLException e) {
			_log.error(sql, e);
			throw new ProjectXSQLException(e);
		} finally {
			if (rs != null)
				try {
					rs.close();
				} catch (SQLException e) {
				}
			if (ps != null)
				try {
					ps.close();
				} catch (SQLException e) {
				}
		}
	}

	public String getString(Object value) {
		if (value == null) {
			return "";
		}
		return StringUtil.trim(value.toString());
	}

	private void update(String cursorName) throws ProjectXSQLException {

		String sql = "UPDATE t_ahe_user SET last_login_timestamp = '1' WHERE CURRENT OF "
				+ cursorName;

		PreparedStatement ps = null;

		try {

			if (_log.isDebugEnabled()) {
				_log.debug(sql);
			}

			ps = _conn.prepareStatement(sql);

			ps.executeUpdate();
		} catch (SQLException e) {
			_log.error(sql, e);
			throw new ProjectXSQLException(e);
		} finally {
			if (ps != null)
				try {
					ps.close();
				} catch (SQLException e) {
				}
		}
	}

}
